package com.nuanshui.heatedloan.baseservice.service;

import com.nuanshui.framework.res.ResourceFacade;
import com.nuanshui.framework.springContext.SpringContextUtil;
import com.nuanshui.framework.springContext.SpringPropertiesUtil;
import com.nuanshui.framework.utils.LogUtil;
import com.nuanshui.heatedloan.baseservice.dal.mapper.AdvertisementMapper;
import com.nuanshui.heatedloan.baseservice.facade.AdvertisementService;
import com.nuanshui.heatedloan.baseservice.facade.ImageService;
import com.nuanshui.heatedloan.constant.DubboCacheRegion;
import com.nuanshui.heatedloan.constant.ServiceConstant;
import com.nuanshui.heatedloan.constant.TransactionManagerType;
import com.nuanshui.heatedloan.dal.module.Advertisement;
import com.nuanshui.heatedloan.dal.vo.AdvertisementVO;
import com.nuanshui.heatedloan.dubbo.extension.cache.manager.DubboCacheManager;
import com.nuanshui.heatedloan.model.Image;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service(ServiceConstant.ADVERTISEMENTSERVICE)
@Transactional(readOnly=true,  value = TransactionManagerType.BASE_SERVICE_TRANSACTION_MANAGER)
public class AdvertisementServiceImpl implements AdvertisementService{
	
	private static final Logger logger  = LoggerFactory.getLogger(AdvertisementServiceImpl.class);
	
	@Autowired 
	private AdvertisementMapper advertisementMapper;

	@Autowired
	private ImageService imageService;
	
	@Autowired
	private ResourceFacade resourceFacade;
	
	@Autowired
	private DubboCacheManager dubboCacheManager;


	private AdvertisementService advertisementService;
	
	private AdvertisementService getAdvertisementService(){
		if(advertisementService == null){
			advertisementService =  SpringContextUtil.getBean(AdvertisementService.class);
		}
		return advertisementService;
	}
	
	@Override
	@Transactional(value = TransactionManagerType.BASE_SERVICE_TRANSACTION_MANAGER)
	public void save(Advertisement entity){
		advertisementMapper.save(entity);
	}
	
	@Override
	public AdvertisementVO loadVO(String id){
		AdvertisementVO advertisementVO = advertisementMapper.loadVO(id);
		//处理图片数据
		Image image = imageService.load(advertisementVO.getImageId());
		advertisementVO.setImage(image);
		return advertisementVO;
	}
	
	@Override
	public List<AdvertisementVO> findByBizTypeAndBizId(Integer bizType,String bizId){
		List<AdvertisementVO> list = advertisementMapper.findByBizTypeAndBizId(bizId,bizType);
		for(AdvertisementVO advertisementVO : list){
			Image image = imageService.load(advertisementVO.getImageId());
			advertisementVO.setImage(image);
		}
		return list;
	}

	@Override
	public List<AdvertisementVO> findByBizType(Integer bizType)
	{
        return getAdvertisementService().findByBizTypeAndBizId(bizType,null);
	}

	@Override
	public Image findFirstListImage(String bizId, Integer bizType) {
		List<AdvertisementVO> list = advertisementMapper.findByBizTypeAndBizId(bizId,bizType);
		Image image=null;
		for (AdvertisementVO advertisementVO : list) {
			image = imageService.load(advertisementVO.getImageId());
		}
		return image;
	}

	@Override
	public AdvertisementVO findFirstByBizIdAndBizType(Integer bizType,String bizId) {
		List<AdvertisementVO> list = getAdvertisementService().findByBizTypeAndBizId(bizType,bizId);
		if(!list.isEmpty()){
			return list.get(0);
		}
		return null;
	}

	@Override
	public Integer findBannerOrderNum(String bizId, Integer bizType) {
		return advertisementMapper.findBannerOrderNum(bizId, bizType);
	}

	@Override
	@Transactional(value = TransactionManagerType.BASE_SERVICE_TRANSACTION_MANAGER)
	public void deleteAdvertisement(String bizId, int bizTyp) {
		advertisementMapper.deleteAdvertisement(bizId, bizTyp);
		//清缓存
		dubboCacheManager.evictCache(DubboCacheRegion.CACHE_CONSTANT_ADVERTISEMENTVO, bizId, bizTyp);
	}

	@Override
	public void deleteById(String id) {

	}

	@Override
	@Transactional(value = TransactionManagerType.BASE_SERVICE_TRANSACTION_MANAGER)
	public Advertisement saveAdvertisementByImage(Image image, String bizId, int bizType, int orderNum) {
		Advertisement item = new Advertisement();
		//IdTypeHandler.encode(Long.parseLong(image.getId()))
		item.setImageId(image.getId());
		item.setBizId(bizId);
		item.setBizType(bizType);
		item.setArchive(1);
		item.setOrderNum(orderNum);
		item.setJumpUrl("");
		save(item);
		dubboCacheManager.evictCache(DubboCacheRegion.CACHE_CONSTANT_ADVERTISEMENTVO, bizId, bizType);
		return item;
	}

	@Override
	public AdvertisementVO findDefaultAdvertisementVO() {
		AdvertisementVO advertisementVO=new AdvertisementVO();
		LogUtil.info(logger, "默认图片的module.banner.defaultImageId为"+SpringPropertiesUtil.getPropertiestValue("module.banner.defaultImageId"));
		//Image image = imageService.load(IdTypeHandler.decode(advertisementVO.getImageId())+"");
		Image image = imageService.load((SpringPropertiesUtil.getPropertiestValue("module.banner.defaultImageId"))+"");
	    advertisementVO.setImage(image);
		return advertisementVO;
	}
	
}
